<?php
namespace Home\Controller;
use Think\Controller;

// 开发者：杨晓杰，
// 邮箱：2902468225@qq.com 


// 学生用户控制器，负责学生用户表的相关操作
class UserController extends Controller {
    // 学生用户时调用学生表
	private $dbName='student';
	// 用户登录功能
    public function login(){
        $where['user']=I('post.user');
    	$password=I('post.password');
        $type=1;
    	//$data['user']='2902468225@qq.com';
    	//$data['password']='123456';
        $data=M($this->dbName)->where($where)->find();
    	if(!$data)$result=array('code'=>1,'msg'=>'用户名不存在');
        else if($data['password']!=$password)$result=array('code'=>2,'msg'=>'密码错误');
        else if($data['is_black']==1)$result=array('code'=>3,'msg'=>'您已被列入黑名单');
        else if($data['is_verify']==4)$result=array('code'=>4,'msg'=>'您的账户未激活');
        else{
            $temp=str_replace('.','_point_',$where['user']);
            cookie('tswc_user',$temp.$type);
            session($temp.$type,1);
            $result=array('data'=>cookie('tswc_pre'),'code'=>0,'msg'=>'登录成功');
        }
    	echo json_encode($result);
    }
    // 用户注销功能
    public function logout(){
    	$user=$_COOKIE['tswc_user'];
    	session($user,null);

        $result=array('code'=>0,'msg'=>'注销成功');
        echo json_encode($result);
        // tag('func2');
    }
    
    // 用户注册功能
    public function enroll(){
        tag('func');
    	$data=I('post.');
        // $data['user']='2902468225@qq.com';
        // $data['password']='123456';
    	$data['create_time']=$data['update_time']=date('Y-m-d H:i:s',time());
    	if(M($this->dbName)->where('user = "%s"',$data['user'])->find())$result=array('data'=>'','code'=>1,'msg'=>'邮箱已被注册');
    	else {
            $data['is_verify']=4;
    		if(M($this->dbName)->add($data)){
                $result=array('data'=>'','code'=>0,'msg'=>'注册成功');

                // 发送邮件
                $to=$data['user'];
                $title='天生我才平台';
                $content='注册成功，点击连接<a href="http://'.R('Myfunc/Myfunc/getDomain').'/user/active/code/'.base64_encode($data['user']).'">http://'.R('Myfunc/Myfunc/getDomain').'/user/active/code/'.base64_encode($data['user']).'</a>激活账号';
                R('Myfunc/Myfunc/sendMail',array('to'=>$to,'title'=>$title,'content'=>$content));

            }
    		else $result=array('data'=>'','code'=>2,'msg'=>'注册失败');
    	}
    	echo json_encode($result);
    }
    // 信息修改页面
    public function updatePage(){
        tag('func2');
        $id=$_ENV['user']['id'];
        $data['user']=M($this->dbName)->where('id ='.$id)->find();
        $this->assign('data',$data);
        R('Page/updatePage');
    }
    //修改用户信息
    public function update(){
    	tag('func2');
    	$id=$_ENV['user']['id'];

    	$data=I('post.');
        $photo=R('Myfunc/Myfunc/upload');
        if($photo){
            $data['photo']=$photo;
            
            $oldFile=M($this->dbName)->where($where)->field('photo')->find();
            $oldFile='.'.$oldImg['photo'];
        }

        $data['update_time']=date('Y-m-d H:i:s',time());
    	if(M($this->dbName)->where('id ='.$id)->save($data)!==false){
            $result=array('data'=>'','code'=>0,'msg'=>'修改成功');

            // 删除原来图片
            if(file_exists($oldFile))unlink($oldFile);
        }
    	else $result=array('data'=>'','code'=>1,'msg'=>'修改失败');
    	echo json_encode($result);
    }
    // like公司或学生
    public function like(){
    	tag('func');
        if($_ENV['user']['type']==1){
            // 学生用户时
            $data['std_id']=$_ENV['user']['id'];
            $data['cam_id']=I('post.id');
            $data['like_type']=0;
        }else if($_ENV['user']['type']==2){
            // 企业用户时
            $data['std_id']=I('post.id');
            $data['cam_id']=$_ENV['user']['id'];
            $data['like_type']=1;
        }
        $data['like_msg']=I('post.like_msg');
        $data['like_status']=0;
        $data['like_time']=date('Y-m-d H:i:s',time());

        // 是否已经like成功过
        // $where['std_id']=$data['std_id'];
        // $where['cam_id']=$data['cam_id'];
        // $where['like_type']=$data['like_type'];
        // $where['like_status']=1;
        // if(M('like')->where($where)->find())$result=array('code'=>2,'msg'=>'已经like成功过');

        // 返回给企业的信息
        $msg['cam_id']=$data['cam_id'];
        $msg['title']="有一个学生like了您";
        $msg['contain']='有一个学生LIKE了您，请尽快回复';
        $msg['create_time']=$msg['update_time']=date('Y-m-d H:i:s',time());

        // 发送邮件
        $company=M('campany')->where('id = '.$data['cam_id'])->find();
        $to=$company['email'];
        $title='天生我才平台';
        $content='有一个学生like您，请前往查看';
        R('Myfunc/Myfunc/sendMail',array('to'=>$to,'title'=>$title,'content'=>$content));

        // else{
            // 是否以前发起过like
            // unset($where['like_status']);
            // if(M('like')->where($where)->find()){
            //     if(M('like')->where($where)->save($data))$result=array('code'=>0,'msg'=>'like请求成功');
            //     else $result=array('code'=>1,'msg'=>'like请求失败');
            // }else{
                // 以前没有like过则添加
                if(M('like')->add($data)&&M('cam_msg')->add($msg))$result=array('data'=>'','code'=>0,'msg'=>'like请求成功');
                else $result=array('code'=>1,'msg'=>'like请求失败');
            // }
        // }
        echo json_encode($result);
    }
    // 学生用户同意like
    public function agreeLike(){
        tag('func2');
        $where['like_id']=I('post.id');
        $data['like_status']=1;
        if(M('like')->where($where)->save($data))$result=array('code'=>0,'msg'=>'操作成功');
        else $result=array('code'=>1,'msg'=>'操作失败');
    }
    // 学生用户拒绝like
    public function ignoreLike(){
        tag('func2');
        $where['like_id']=I('post.id');
        $data['like_status']=2;
        if(M('like')->where($where)->save($data))$result=array('code'=>0,'msg'=>'操作成功');
        else $result=array('code'=>1,'msg'=>'操作失败');
    }
    // 学生列表
    public function listPage(){
        tag('func');
        $key=I('get.key');
        // 可按姓名、性别、地区、学校、专业、教育、年级搜索
        $where['name|sex|area|school|professional|education|grade']=array('like',"%".$key."%");
        $where['is_black']=0;

        // 按专业搜索
        $professional=I('get.professional');
        if($professional)$where['professional']=array('like',"%".$professional."%");
        
        // 可按学校搜索
        $school=I('get.school');
        if($school)$where['school']=array('like',"%".$school."%");

        $all=M($this->dbName)->where($where)->order('parise desc')->select();
        $data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));

        //添加是否点赞过
        $length=count($data['list']);
        $where2['ip']=get_client_ip();
        $where2['aim_type']=1;
        for($i=0;$i<$length;$i++){
            $where2['aim_id']=$data['list'][$i]['id'];
            if(M('parise')->where($where2)->find())$data['list'][$i]['is_parise']=1;
            else $data['list'][$i]['is_parise']=0;
        }

        $data['key']=$key;
        $data['professional']=$professional;
        $data['school']=$school;
        if(I('get.json')==1){
            echo json_encode($data);
            return;
        }
        $this->assign('data',$data);
        // dump($data);
        R("Page/stdList");
    }
    // 学生详情
    public function detail(){
        tag('func');
        $id=I('get.id');
        if(!$id)$id=1;
        $data['student']=M($this->dbName)->where('id ='.$id.' and is_black = 0')->find();

        // 企业是否like
        if($_ENV['user']['type']==2){
            if(M('like')->where('like_type = 1 and cam_id = '.$_ENV['user']['id'].' and std_id = '.$id.' and like_status != 2')->find())$data['is_like']=1;
            else $data['is_like']=0;
        }
        // 今天是否点过赞
        $data['is_parise']=R('Myfunc/Myfunc/isParise',array('aim_id'=>$id,'aim_type'=>1));
        
        // 学生作品
        $data['production']=M('std_product')->where('std_id = '.$id)->select();

        $this->assign('data',$data);
        // dump($data);
        R('Page/stdDetail');
    }
    // 点赞
    public function parise(){
    	tag('func');
        $data['aim_id']=I('post.aim_id');
        $data['aim_type']=I('post.aim_type');
        // $data['aim_id']=1;
        // $data['aim_type']=2;
        $data['ip']=get_client_ip();
        $data['parise_time']=time();
        // 判断今天是否点赞过
        if(R('Myfunc/Myfunc/isParise',array('aim_id'=>$data['aim_id'],'aim_type'=>$data['aim_type'])))$result=array('code'=>1,'msg'=>'今天已经点过赞');
        else{
            if($data['aim_type']==1)$db='student';
            else $db='campany';
            $find=M($db)->where('id ='.$data['aim_id'])->field('parise')->find();
            $find['parise']=$find['parise']+1;
            if(M('parise')->add($data)&&M($db)->where('id = '.$data['aim_id'])->save($find))$result=array('data'=>$find['parise'],'code'=>0,'msg'=>'点赞成功');
            else $result=array('code'=>2,'msg'=>'点赞失败');
        }
        echo json_encode($result);
    }

    public function infoUpdate() {
        tag('func2');
        $id=$_ENV['user']['id'];
        $data['user']=M($this->dbName)->where('id ='.$id)->find();

        $data['msgNum']=count(M('std_msg')->where('std_id = '.$id.' and status = 0')->select());
        $this->assign('data',$data);
        // 学生作品
        $data['production']=M('std_product')->where('std_id = '.$id)->select();

        //喜欢我的企业列表
        $data['likeMe']=R('Like/likeMeContent');
        $where['std_id']=$id;
        $where['like_type']=0;
        $where['like_status']=1;
        $len=count($data['likeMe']['list']);
        for($i=0;$i<$len;$i++){
            $where['cam_id']=$data['likeMe']['list'][$i]['cam_id'];
            if(M('like')->where($where)->find())$data['likeMe']['list'][$i]['have_like']=1;
            else $data['likeMe']['list'][$i]['have_like']=0;
        }


        // dump($data['likeMe']);
        R('Page/infoUpdate');
    }
    // 用户个人中心
    public function personCenter(){
        tag('func2');
        $id=$_ENV['user']['id'];
        $data['user']=M($this->dbName)->where('id ='.$id)->find();

        $data['msgNum']=count(M('std_msg')->where('std_id = '.$id.' and status = 0')->select());
        $this->assign('data',$data);
        // 学生作品
        $data['production']=M('std_product')->where('std_id = '.$id)->select();

        //喜欢我的企业列表
        $data['likeMe']=R('Like/likeMeContent');
        $where['std_id']=$id;
        $where['like_type']=0;
        $where['like_status']=1;
        $len=count($data['likeMe']['list']);
        for($i=0;$i<$len;$i++){
            $where['cam_id']=$data['likeMe']['list'][$i]['cam_id'];
            if(M('like')->where($where)->find())$data['likeMe']['list'][$i]['have_like']=1;
            else $data['likeMe']['list'][$i]['have_like']=0;
        }


        // dump($data['likeMe']);
        R('Page/personCenter');
    }
    // 个人荣誉
    public function honor(){
        tag('func2');
        $id=$_ENV['user']['id'];
        $data['user']=M($this->dbName)->where('id ='.$id)->find();

        $this->assign('data',$data);
        
        // dump($data['likeMe']);
        R('Page/honor');
    }
    // 消息通知列表
    public function msgList(){
        tag('func2');
        // $where['status']=I('get.is_read')?I('get.is_read'):0;//0未读，1已读
        $where['std_id']=$_ENV['user']['id'];
        $all=M('std_msg')->where($where)->order('create_time desc')->select();
        $data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));
        $data['is_read']=$where['status'];
        $this->assign('data',$data);
        // dump($data);
        R('Page/msgList');
    }
    // 消息详情
    public function msgDetail(){
        tag('func2');
        $where['id']=I('get.id');
         // 标记为已读
        $read['status']=1;
        M('std_msg')->where($where)->save($read);
        $data['msg']=M('std_msg')->where($where)->find();
        $this->assign('data',$data);
        // dump($data);
        R('Page/msgDetail');
    }
    //信息已读,不传id则所有未读标记为已读
    public function msgRead(){
        tag('func2');
        if(I('post.id'))$where['id']=I('post.id');
        $where['std_id']=$_ENV['user']['id'];
        $where['status']=0;
        $data['status']=1;
        if(M('std_msg')->where($where)->save($data))$result=array('code'=>0,'msg'=>'操作成功');
        else $result=array('code'=>1,'msg'=>'操作失败');
        echo json_encode($result);
    }
    // 删除信息
    public function msgDelete(){
        tag('func2');
        $where['id']=I('post.id');
        $where['std_id']=$_ENV['user']['id'];
        if(M('std_msg')->where($where)->delete())$result=array('code'=>0,'msg'=>'操作成功');
        else $result=array('code'=>1,'msg'=>'操作失败');
        echo json_encode($result);
    }
    // 找回密码页面
    public function findPswdPage(){
        tag('func');
        R('Page/findPswd');
    }
    // 找回密码接口
    public function findPswd(){
        $where['user']=I('post.user');
        if(!$where['user']){
            echo json_encode(array('code'=>1,'msg'=>'用户名不存在'));
            return;
        }
        if(!M($this->dbName)->where($where)->find()){
            echo json_encode(array('code'=>2,'msg'=>'用户名不存在'));
            return;
        }
        $newPswd['password']=rand(100000,999999);
        $to=$where['user'];
        $content='您的密码是'.$newPswd['password'];
        M($this->dbName)->where($where)->save($newPswd);
        R('Myfunc/Myfunc/sendMail',array('to'=>$to,'title'=>'天生我才平台','content'=>$content));
        echo json_encode(array('code'=>0,'msg'=>'新密码已发送到您的邮箱'));
    }
    // 修改密码页面
    public function changePswdPage(){
        tag("func2");
        $where['id']=$_ENV['user']['id'];
        $data=M($this->dbName)->where($where)->field('password')->find();
        $this->assign('data',$data);
        // dump($data);
        R('Page/changePswd');
    }
    // 修改密码
    public function changePswd(){
        tag('func2');
        $where['id']=$_ENV['user']['id'];

        // 旧密码判断
        $oldPswd=I('post.oldPswd');
        $find=M($this->dbName)->where($where)->field('password')->find();
        if($oldPswd!=$find['password'])$result=array('code'=>2,'msg'=>'旧密码错误');
        
        else{
            $data['password']=I('post.password');
            $data['update_time']=date('Y-m-d H:i:s', time());
            if(M($this->dbName)->where($where)->save($data)!==false)$result=array('code'=>0,'msg'=>'修改成功');
            else $result=array('code'=>1,'msg'=>'修改失败');
        }
        
        echo json_encode($result);
    }
    // 隐藏信息开关接口
    public function hideInfoToggle(){
        tag('func2');
        $where['id']=$_ENV['user']['id'];
        $is_open=M($this->dbName)->where($where)->field('is_open')->find();
        if($is_open['is_open']==0)$data['is_open']=1;
        else $data['is_open']=0;
        if(M($this->dbName)->where($where)->save($data))$result=array('code'=>0,'msg'=>'成功');
        else $result=array('code','msg'=>'失败');
        echo json_encode($result);
    }
    // 短信邮箱开关接口
    public function mailToggle(){
        tag('func2');
        $where['id']=$_ENV['user']['id'];
        $is_sms_email=M($this->dbName)->where($where)->field('is_sms_email')->find();
        if($is_sms_email['is_sms_email']==0)$data['is_sms_email']=1;
        else $data['is_sms_email']=0;
        if(M($this->dbName)->where($where)->save($data))$result=array('code'=>0,'msg'=>'成功');
        else $result=array('code','msg'=>'失败');
        echo json_encode($result);
    }
    // 学生认证状态页面
    public function verifyStatus(){
        tag('func2');
        $where['id']=$_ENV['user']['id'];
        $data=M($this->dbName)->where($where)->field('is_verify')->find();
        $this->assign('data',$data);
        // dump($data);
        R('Page/verifyStatus');
    }
    // 学生提交认证页面
    public function sendVerifyPage(){
        tag('func2');
        $where['id']=$_ENV['user']['id'];
        $data=M($this->dbName)->where($where)->find();
        $this->assign('data',$data);
        // dump($data);
        R('Page/sendVerify');
    }
    // 学生提交认证接口
    public function sendVerify(){
        tag('func2');
        $where['id']=$_ENV['user']['id'];
        //学生姓名，性别，电话，邮箱，学校，学生证，身份证，
        $data=I('post.');
        $upload=R('Myfunc/Myfunc/upload');
        if($upload[0])$data['stdCard']=$upload[0];
        if($upload[1])$data['idCard']=$upload[1];
        $data['update_time']=date('Y-m-d H:i:s',time());
        $data['is_verify']=0;
        if(M($this->dbName)->where($where)->save($data))$result=array('code'=>0,'msg'=>'成功');
        else $result=array('code'=>1,'msg'=>'失败');
        echo json_encode($result);
    }
    // 评论
    public function comment(){
        tag('func2');
        //方式：post,参数：cpn_id公司id，content评论内容
        $cpn_id=I('post.cpn_id');
        $std_id=$_ENV['user']['id'];
        $content=I('post.content');
        if(R('Myfunc/Comment/comment',
            array('cpn_id'=>$cpn_id,
                'std_id'=>$std_id,
                'content'=>$content)))$result=array('code'=>0,'msg'=>'评论成功');
        else $result=array('code'=>1,'msg'=>'评论失败');
        echo json_encode($result);
    }
    // 相关人才，按专业推荐
    public function relev(){
        if(I('get.professional'))$where['professional']=array('like','%'.I('get.professional').'%');
        $all=M($this->dbName)->where($where)->order('parise desc')->select();
        $data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));
        echo json_encode($data);
    }
    //激活
    public function active(){
        $where['user']=base64_decode(I('get.code'));
        // 设置为激活状态
        $data['is_verify']=3;
        M($this->dbName)->where($where)->save($data);

        //设置cookie和session
        $type=1;
        $temp=str_replace('.','_point_',$where['user']);
        cookie('tswc_user',$temp.$type);
        session($temp.$type,1);
        // 跳转到个人中心
        echo '<script>location.href="/user/personCenter"</script>';
    }
    // 修改邮箱页面
    public function emailChangePage(){
        tag('func2');
        R('Page/emailChange');
    }
    // 修改邮箱接口
    public function emailChange(){
        tag('func2');
        $where['id']=$_ENV['user']['id'];

        $emailNew=I('post.email');
        if(!$emailNew){
            echo json_encode(array('code'=>1,'msg'=>'邮箱不能为空'));
            return;
        };
        if(M($this->dbName)->where('user = "%s"',$emailNew)->find()){
            echo json_encode(array('code'=>2,'msg'=>'邮箱已被注册'));
            return;
        }
        $user=M($this->dbName)->where($where)->find();

        $p1=base64_encode($user['id']);
        $p2=base64_encode($emailNew);
        
        $url='http://'.$_SERVER['HTTP_HOST'].'/user/emailCheck?p1='.$p1.'&p2='.$p2;
        // dump($url);
        // die();
        $content='点击链接修改邮箱<a href="'.$url.'">'.$url.'</a>';
        R('Myfunc/Myfunc/sendMail',array('to'=>$user['email'],'title'=>'天生我才平台','content'=>$content));
        echo json_encode(array('code'=>0,'msg'=>'已发送邮件，确认后修改邮箱'));
    }
    // 修改邮箱确认接口
    public function emailCheck(){
        $where['id']=base64_decode(I('get.p1'));
        $emailNew['user']=base64_decode(I('get.p2'));
        $emailNew['email']=base64_decode(I('get.p2'));
        if(!$where['id']||!$emailNew)return;
        M($this->dbName)->where($where)->save($emailNew);
        $url='http://'.$_SERVER['HTTP_HOST'].'/user/personcenter';
        echo '<script>location.href="'.$url.'"</script>';
    }
}